package edu.uw.tcss360.group6.gui.MainCards;
import edu.uw.tcss360.group6.gui.GUI;
import edu.uw.tcss360.group6.model.*;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

@SuppressWarnings("serial")
/**
 * Author: Mais Abdulwahab
 * Author: Landis Huey
 * Author: Bill Minter
 * Date: 2/25/13
 * 
 */
public class RegisterPanel extends MainCard {
	private final JButton my_register;
	private final JTextField my_name_field;
	private final JTextField my_user_name_field;
	private final JTextField my_phone_number_field;
	private final JTextField my_address_field;
	private final JTextField my_email_field;
	private String my_role;
	//private Category my_category;
	private String my_category_name;
	private JComboBox my_category_list;
	
	
	public RegisterPanel(){
		super();
		this.setLayout(new GridLayout(7,2));
		
		my_name_field = new JTextField();
		my_user_name_field = new JTextField();
		my_phone_number_field = new JTextField();
		my_address_field = new JTextField();
		my_email_field = new JTextField();
		
		my_register = new JButton("Register");
		my_register.addActionListener(new ActionListener()
		{
			@SuppressWarnings("static-access")
			public void actionPerformed(final ActionEvent an_event)
			{
				//I reformatted this method to avoid the logic error that was in here before - Bill
				String name = my_name_field.getText();
				String phone_number = my_phone_number_field.getText();
				String user_name = my_user_name_field.getText();
				String address = my_address_field.getText();
				String email = my_email_field.getText();
				
				if(name.isEmpty()) {
					JOptionPane.showMessageDialog(null, "Enter a name!");
				} else if(user_name.isEmpty()) {
					JOptionPane.showMessageDialog(null, "Enter a user name!");				
				} else if(phone_number.isEmpty()) {
					JOptionPane.showMessageDialog(null, "Enter a phone number!");			
				} else if(address.isEmpty()) {
					JOptionPane.showMessageDialog(null, "Enter an address!");
				
				} else if(email.isEmpty() || !email.contains("@")) {
					JOptionPane.showMessageDialog(null, "Enter an email!");
				} else {
						JOptionPane.showMessageDialog(null, "Sucessfully Registered!");
						GUI.instance().my_main_panel.setCard("splash");
						User temp_user = new User(user_name, name, phone_number, address, email);// Added -Mais
						if(my_role == null)
						{			
							Contest.instance().addUser(temp_user);// Added -Mais
							GUI.instance().currentUser = temp_user;// Added -Mais
						}
						if(my_role == "attendee")
						{
							Contest.instance().addUser(temp_user);// Added -Mais
							GUI.instance().currentUser = temp_user;// Added -Mais
						}
						if(my_role == "contestant")
						{
							User temp = new Contestant(temp_user);// Added -Mais
							Contest.instance().addUser(temp);// Added -Mais
							GUI.instance().currentUser = temp;// Added -Mais
						}
						if(my_role == "judge")
						{
							User temp = new Judge(temp_user,new Category(my_category_name));// Added -Mais
							Contest.instance().addUser(temp);// Added -Mais
							GUI.instance().currentUser = temp;// Added -Mais
						}
						if(my_role == "organizer")
						{
							User temp = new Organizer(temp_user);// Added -Mais
							Contest.instance().addUser(temp);// Added -Mais
							GUI.instance().currentUser = temp;// Added -Mais
						}
						try {
							Contest.instance().save();
						} catch (IOException e) {
							e.printStackTrace();
							System.out.println("Couldn't save the new user");
						}
					}
				
			}
			
		});
	    
		addParts();
		
		
	}
	
	private void addParts()
	{
		addElement(new JLabel("Name:"));
		addElement(my_name_field);
		addElement(new JLabel("User Name:"));
		addElement(my_user_name_field);
		addElement(new JLabel("Phone Number:"));
		addElement(my_phone_number_field);
		addElement(new JLabel("Address:"));
		addElement(my_address_field);
		addElement(new JLabel("Email:"));
		addElement(my_email_field);
		addElement(new JLabel("Role:"));
		addRoles();
		addCategories();
		add(my_register, BorderLayout.SOUTH);
		setBorder(BorderFactory.createEmptyBorder(100, 20, 200, 10));
	}
	private void addElement(JComponent c) {
        c.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.add(c);
    }
	 private void addCategories()
	    {
	    	Category[] cat = Contest.instance().getCategories();
	    	String[]temp = new String[cat.length];
	    	for(int i = 0; i < cat.length; i++)
	    	{
	    		temp[i] = cat[i].getName();
	    	}
	    	my_category_list = new JComboBox(temp);
	    	my_category_list.setEnabled(false);
	    	my_category_list.addActionListener(new ActionListener()
	    	{
	    		public void actionPerformed(final ActionEvent an_event)
	    		{
	    			JComboBox cb = (JComboBox)an_event.getSource();
	    	        my_category_name = (String)cb.getSelectedItem();
	    		}
	    		
	    	});
	    	addElement(my_category_list);
	    }

	private void addRoles()
	{
		//changed this b/c it led to a bug later on and attendees have no reason to 
		//register.
    	String[]temp = {"attendee","contestant", "organizer", "judge"};
		//String[]temp = {"contestant", "organizer", "judge"};
    	JComboBox role_list = new JComboBox(temp);
    	role_list.addActionListener(new ActionListener()
    	{
    		public void actionPerformed(final ActionEvent an_event)
    		{
    			JComboBox cb = (JComboBox)an_event.getSource();
    	        my_role = (String)cb.getSelectedItem();
    	        if(my_role == "judge")
    	        {
    	        	my_category_list.setEnabled(true);
    	        }
    		}
    	});
    	addElement(role_list);
	}
}
